__fillInGUI.validate = function()
{
    var notFill = [];
    var tmpNotFill = [];
    tmpNotFill[0] = false;
    for (var i = 0; i < __fillInGUI.env.questions_num; i++) {
        if (__fillInGUI.env.opposite_mode) {
            for (var j = 0; j < __fillInGUI.env.checksNumber; j++) {
                tmpNotFill[tmpNotFill.length] = false;
            }
        } else {
            tmpNotFill[tmpNotFill.length] = false;
        }
    }
    var form = document.getElementById("fill_in_form");
    var prefix = "data[answers][";
    if (form.elements.length) {
        for (var i = 0; i < form.elements.length; i++) {
            var element = form.elements[i];
            if ((element.type == "radio") && element.checked && (element.name.indexOf(prefix) == 0)) {
                var number = parseInt(element.name.substr(prefix.length));
                if (__fillInGUI.env.opposite_mode) {
                    number = (number - 1) * __fillInGUI.env.checksNumber
                        + parseInt(element.name.substr(element.name.lastIndexOf("[") + 1));
                }
                if (number) {
                    tmpNotFill[number] = true;
                }
            }
        }
    }
    for (var i = 1; i < tmpNotFill.length; i++) {
        if (! tmpNotFill[i]) {
            notFill[notFill.length] = i;
        }
    }
    if (notFill.length == 0) {
        return true;
    } else {
        __fillInGUI.hightlightErrors(notFill);
    }
    if (__fillInGUI.env.is_quest) {
        alert(__fillInGUI.env.po.selectAllAnsfers);
    } else {
        return confirm(__fillInGUI.env.po.youNotFillNextAnsfers + notFill.join(", ") + __fillInGUI.env.po.ContimueConfirm);
    }
    return false;
}

__fillInGUI.hightlightErrors = function(raw)
{
    for (var i = 0; i < raw.length; i++) {
        var id = "question_";
        if (__fillInGUI.env.opposite_mode) {
            var sub_index = raw[i] % __fillInGUI.env.checksNumber;
            if (sub_index == 0) {
                sub_index = __fillInGUI.env.checksNumber;
            }
            var main_index = ((raw[i] - sub_index) / __fillInGUI.env.checksNumber) + 1;
            id += main_index + "_" + sub_index;
        } else {
            id += raw[i];
        }
        $(document.getElementById(id)).addClass("invalid");
    }
}

__fillInGUI.check = function(elem, event)
{
    var input = elem.getElementsByTagName('input')[0];
    var was_checked = input.checked;
    if (event.target != elem) {
        if (__fillInGUI.down == input) {
            was_checked = __fillInGUI.was_checked;
        } else {
            __fillInGUI.down = null;
        }
    }
    input.checked = true;
    input.focus();
    if (__fillInGUI.env.opposite_mode) {
        var separator_pos = input.name.lastIndexOf("[");
        var name = input.name.substr(0, separator_pos);
        var sum_number = parseInt(input.name.substr(separator_pos + 1));
        for (var k = 1; k <= __fillInGUI.env.checksNumber; k++) {
            if (k != sum_number) {
                var opposite = getRadioByNameAndValue(input.form, name + "[" + k + "]", input.value);
                if (opposite.checked) {
                    opposite.checked = false;
                    var siblings = $(opposite).parents(".question").contents(".answer_cell");
                    siblings.removeClass("selected_answer");
                }
            }
        }
    }

    var prefix = "data[answers][";
    var indexOfSubNumber = input.name.substr(prefix.length, input.name.length).indexOf("][");
    var isCombinationMode = __fillInGUI.env.bin_combs_mode && (indexOfSubNumber > 0);
    indexOfSubNumber += prefix.length;

    var parent = $(elem).parents(".question");
    parent.removeClass("invalid");
    var siblings = parent.contents(".answer_cell");
    siblings.removeClass("hover_answer");
    if (! isCombinationMode) {
        siblings.removeClass("selected_answer");
    }
    if (! __fillInGUI.env.is_quest && was_checked) {
        input.checked = false;
        $(elem).removeClass("selected_answer");
    } else {
        $(elem).addClass("selected_answer");
    }

    if (isCombinationMode && input.checked) {
        indexOfSubNumber += 2;
        var self_number = input.value;
        var prefix2 = input.name.substr(0, indexOfSubNumber);
        var otherCheckedButtons = [];
        for (var k = 1; k <= __fillInGUI.env.answersNumber; k++) {
            if (k != self_number) {
                var otherButton = getRadioByNameAndValue(input.form, prefix2 + k + "]", k);
                if (otherButton.checked) {
                    otherCheckedButtons[otherCheckedButtons.length] = otherButton;
                } else {
                    $(otherButton).parents(".answer_cell").removeClass("selected_answer");
                }
            }
        }
        if (otherCheckedButtons.length) {
            var clearOther = false;
            if (otherCheckedButtons.length == 1) {
                var allowNumbers = __fillInGUI.env.binaryCombinations[otherCheckedButtons[0].value];
                clearOther = true;
                for (var i = 0; i < allowNumbers.length; i++) {
                    if (allowNumbers[i] == input.value) {
                        clearOther = false;
                        break;
                    }
                }
            } else {
                clearOther = true;
            }
            if (clearOther) {
                for (var i = 0; i < otherCheckedButtons.length; i++) {
                    otherCheckedButtons[i].checked = false;
                    $(otherCheckedButtons[i]).parents(".answer_cell").removeClass("selected_answer");
                }
            }
        }
    }
}

__fillInGUI.over = function(elem, event)
{
    if (! $(elem).hasClass("selected_answer")) {
        $(elem).addClass("hover_answer");
    }
}

__fillInGUI.out = function(elem, event)
{
    $(elem).removeClass("hover_answer");
}

__fillInGUI.radioDown = function(target, event)
{
    __fillInGUI.down = target;
    __fillInGUI.was_checked = target.checked;
}

function getRadioByNameAndValue(form, name, value)
{
    if (typeof name != 'string') {
        throw "formValueOf(): Bad argument “name”. Must be string.";
    }
    if (form.elements) {
        var element = form.elements[name];
        if (element == undefined) {
            return undefined;
        }
        if (element.length) {
            for (var i = 0; i < element.length; i++) {
                var button = element[i];
                if ((button.type == 'radio') && (button.value == value)) {
                    return button;
                }
            }
        } else if ((element.type == 'radio') && (element.value == value)) {
            return element;
        }
    } else {
        throw "formValueOf(): Bad argument “form”. Must be instance of HTMLFormElement.";
    }

    return null;
}